﻿using AutoMapper;
using DFGK_WMS.Context;
using DFGK_WMS.IRepository;
using DFGK_WMS.Model.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace DFGK_WMS.Repository
{
    public class TMaterialKeyRepository : BaseRepository<TMaterialKey>, ITMaterialKeyRepository
    {
        public TMaterialKeyRepository(DFGK_DB context, IMapper mapper) : base(context, mapper)
        {
        }

        /// <summary>
        /// 添加物料批次
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        public bool AddTMaterialKey(TMaterialKey m)
        {
            var obj = Context.TMaterialKeys.AsNoTracking().FirstOrDefault(x => x.BatchNo == m.BatchNo);
            if (obj != null)
            {
                return false;
            }
            //质量状态设定为合格
            m.QualityState = 1;
            m.CreateDate= DateTime.Now;
            Context.TMaterialKeys.Add(m);
            return Context.SaveChanges() > 0;
        }

        /// <summary>
        /// 删除物料批次
        /// </summary>
        /// <param name="mid"></param>
        /// <returns></returns>
        public bool DelTMaterialKey(int mid)
        {
            var mobj = Context.TMaterialKeys.Find(mid);

            if (mobj != null) 
            {
                Context.TMaterialKeys.Remove(mobj);
            }
            return Context.SaveChanges() > 0;
        }

        /// <summary>
        /// 根据主键返回对象
        /// </summary>
        /// <param name="mid"></param>
        /// <returns></returns>
        public TMaterialKey GetTMaterialKeyById(int mid)
        {
            return Context.TMaterialKeys.Find(mid);
        }

        /// <summary>
        /// 显示物料批次列表
        /// </summary>
        /// <param name="mname"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalCount"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public List<TMaterialKey> GetTMaterialKeyList(string mname, int pageIndex, int pageSize, out int totalCount)
        {
            var list = Context.TMaterialKeys.AsNoTracking().AsQueryable();

            if(!string.IsNullOrEmpty(mname))
            {
                list = list.Where(x => x.MaterialName.Contains(mname));
            }

            totalCount = list.Count();

            return list.OrderByDescending(x => x.CreateDate).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
        }

        /// <summary>
        /// 修改物料批次
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        public bool UpdTMaterialKey(TMaterialKey m)
        {
            var list = Context.TMaterialKeys.AsNoTracking().FirstOrDefault(x => x.Id != m.Id && x.BatchNo == m.BatchNo);

            if (list != null)
            {
                return false;
            }

            Context.TMaterialKeys.Update(m);
            return Context.SaveChanges() > 0;
        }
        /// <summary>
        /// 导出物料批次
        /// </summary>
        /// <returns></returns>
        public List<TMaterialKey> GetTmaterExcel()
        {
            try
            {
                var list = Context.TMaterialKeys.AsQueryable();
                return list.ToList();
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 带条件查询物料批次
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public List<TMaterialKey> GetMaterialKeyList(Expression<Func<TMaterialKey, bool>> where)
        {
            return Context.TMaterialKeys.Where(where).ToList();
        }
    }
}
